/* global zk, zAu, zul, jq */

function hiddenIconOpenCloseNodeChilds(component) {
    var list = document.getElementById(component.uuid).getElementsByClassName('z-tree-icon');
    for (i = 0; i < list.length; i++) {
        list[i].style.visibility = 'hidden';
        list[i].style.width = '0px';
        list[i].style.height = '0px';
    }
}

function controlClick(w) {
    jq(w.$n()).click(function (ev) {
        zAu.send(new zk.Event(w, "onClick"));
    });
}

function bindScroller(w, options) {
    var defaultOptions =
            {
                cursorcolor: 'rgb( 46, 142, 237 )',
                cursoropacitymax: 0.6,
                cursorwidth: 12
            };

    var opts = jq.extend({}, defaultOptions, options || {});

    if (w && w.$n()) {
        jq(w.$n()).niceScroll(opts);

        jq(w.$n()).mouseover(function () {
            jq(w.$n()).getNiceScroll().resize();
        });
    }

}

function unbindScroller(w, dt, skipper, after) {
    if (w && w.$n()) {
        jq(w.$n()).niceScroll().remove();

        w.$supers(zul.wgt.Div, 'unbind_', dt, skipper, after);
    }
}

function controlDrag(w, arrowId) {

    $(w.$n()).mousemove(function (e) {
        if (!$('.z-drop-ghost.z-drop-allow').length) {
            return;
        }

        var $this = $(this);

        $this.children().children().children('.z-treecell-text').css('background-color', '#e7f4f9');

        var height = $this.height();
        var lastIcon = $this.children().children().children().children().last();
        var rowTop = lastIcon.offset().top;
        var rowLeft = lastIcon.offset().left;

        var arrow = $('#' + arrowId);

        arrow.css({
            'border-top': '5px solid transparent',
            'border-bottom': '5px solid transparent',
            'border-left': '5px solid black',
            'position': 'absolute',
            'z-index': '999',
            'visibility': 'visible'
        });

        if (e.offsetY < 10) {
            // cima
            arrow.css({
                'top': rowTop + 2 + 'px',
                'left': (rowLeft - 8) + 'px'
            });
        }
        else if (e.offsetY > 20) {
            // baixo 
            arrow.css({
                'top': (rowTop + height - 12) + 'px',
                'left': (rowLeft - 8) + 'px'
            });
        }
        else {
            // meio
            arrow.css({
                'top': (rowTop + (height / 2) - 6) + 'px',
                'left': (rowLeft - 4) + 'px'
            });
        }
    });

    $(w.$n()).mouseout(function (e) {
        $('#' + arrowId).css('visibility', 'hidden');

        $(this).children().children().children('.z-treecell-text').css('background-color', 'transparent');
    });
}

//trumbowyg
function formatTextField(w, json) {
    var trumbowyg = $(w.$n()).trumbowyg({
        btns: [
//            'viewHTML',
//            '|', 
            'btnGrp-design'
        ],
        lang: 'pt',
        fullscreenable: false
    });

    trumbowyg.on('tbwblur', function () {
        zAu.send(new zk.Event(w, "onBlur", trumbowyg.html()));
    });

    setTextInFormatTextField(w.$n(), json);
}

function setTextInFormatTextField(uuid, json) {
    $(uuid).trumbowyg().html(json.value);
}